/**
 * Copyright (c) 2024,  科泰集团教研团队
 * 版权所有，请勿侵权
 */
package com.ktjiaoyu.news.dao.impl;

import com.ktjiaoyu.news.dao.BaseDao;
import com.ktjiaoyu.news.dao.NewsDao;
import com.ktjiaoyu.news.entity.News;
import com.ktjiaoyu.news.entity.NewsType;
import com.ktjiaoyu.news.mapper.NewsMapper;
import com.ktjiaoyu.news.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 新闻
 *
 * @author 科泰集团教研团队
 * @version 1.0.0
 * @createTime 2024-4-17 20:49:54
 * @lastModify 2024-4-17 20:49:54
 */
public class NewsDaoImpl extends BaseDao implements NewsDao {

    @Override
    public List<News> getPage(News entity, Integer pageNo, Integer pageSize) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        System.out.println();
        List<News> list = sqlSession.getMapper(NewsMapper.class).getPage(entity, (pageNo - 1) * pageSize, pageSize);
        MyBatisUtil.closeSqlSession(sqlSession);
        return list;
    }

    @Override
    public int count(News entity) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        int count = sqlSession.getMapper(NewsMapper.class).count(entity);
        MyBatisUtil.closeSqlSession(sqlSession);
        return count;
    }

    @Override
    public List<News> getAll() {
        List<News> list = new ArrayList<>();
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        list = sqlSession.getMapper(NewsMapper.class).getAll();
        return list;
    }

    @Override
    public News getById(Long id) {
        News news = null;
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        news = sqlSession.getMapper(NewsMapper.class).getById(id);
        return news;
    }

    @Override
    public int insert(News entity) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        int result = sqlSession.getMapper(NewsMapper.class).insert(entity);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return result;
    }

   @Override
    public int insertBatch(News... entities) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        int result = sqlSession.getMapper(NewsMapper.class).insertBatch(entities);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
       return result;
    }

    @Override
    public int update(News entity) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        int result = sqlSession.getMapper(NewsMapper.class).update(entity);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return result;
    }

    @Override
    public int deleteByEntity(News entity) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        int result = sqlSession.getMapper(NewsMapper.class).deleteByEntity(entity);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return result;
    }

    @Override
    public int deleteById(Long id) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        int result = sqlSession.getMapper(NewsMapper.class).deleteById(id);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return result;
    }

    @Override
    public int deleteBatch(Long... ids) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        int result = sqlSession.getMapper(NewsMapper.class).deleteBatch(ids);
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return result;
    }

    private String createPageCondition(News entity, List<Object> params) {
        String sql = "";
        if (entity != null) {
            if (entity.getId() != null) {
                sql += " and id = ?";
                params.add(entity.getId());
            }
            if (entity.getTypeId() != null) {
                sql += " and typeId = ?";
                params.add(entity.getTypeId());
            }
            if (entity.getTitle() != null && !entity.getTitle().trim().isEmpty()) {
                sql += " and title like ?";
                params.add("%" + entity.getTitle() + "%");
            }
            if (entity.getContent() != null && !entity.getContent().trim().isEmpty()) {
                sql += " and content = ?";
                params.add(entity.getContent());
            }
            if (entity.getReadCount() != null) {
                sql += " and readCount = ?";
                params.add(entity.getReadCount());
            }
            if (entity.getAuthor() != null && !entity.getAuthor().trim().isEmpty()) {
                sql += " and author like ?";
                params.add("%" + entity.getAuthor() + "%");
            }
            if (entity.getPubTime() != null) {
                sql += " and pubTime = ?";
                params.add(entity.getPubTime());
            }
            if (entity.getAdminId() != null) {
                sql += " and adminId = ?";
                params.add(entity.getAdminId());
            }
        }
        return sql;
    }
}
